using Autofac;
using DotNetExtensions.Core.Autofac;
using DotNetExtensions.Core.MVC;
using DotNetExtensions.Core.WebApi.Filters;
using DotNetExtensions.Core.WebApi.Jwt;
using DotNetExtensions.Core.WebApi.Middlewares;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using SqlSugarSimple.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace SqlSugarSimple
{
    public class Startup
    {
        private readonly AppSettings AppSettings;
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;


            AppSettings = AppSettingDefault<AppSettings>.Default.AppSetings;
        }

        public IConfiguration Configuration { get; }
        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegistTypes(option =>
            {
                option.Pattern.Add("DotNetExtensions.*.dll");
            });
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthorization();
            services.AddMvc();
            services.AddTransient<GlobalExceptionHandler>();
            services.AddControllers(option =>
            {
                option.Filters.Add(new GlobalExceptionFilter());
            }).AddNewtonsoftJson(option =>
            {
                option.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                option.SerializerSettings.Converters.Add(new IsoDateTimeConverter()
                {
                    DateTimeFormat = "yyyy/MM/dd HH:mm:ss"
                });
            });
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builer =>
                {
                    builer.AllowAnyMethod()
                    .AllowAnyHeader()
                    .WithOrigins("*");
                });
            });
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "SqlSugarSimple", Version = "v1" });
            });
            services.AddJwt<UserModel>(JwtOption.Default);
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "SqlSugarSimple v1"));
            }
            app.UseMiddleware<GlobalExceptionHandler>();
            app.UseHttpsRedirection();

            app.UseAutofac(RegistTypeResolver.Default);
            app.UseCors("CorsPolicy");
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "api/{controller}/{action}/{id?}");
                endpoints.MapAreaControllerRoute(
                    name: "area",
                    areaName: "area",
                    pattern: "{area}/{controller}/{action}");
            });
        }
    }
}
